package com.csu.mining.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.csu.mining.R;

public class DocumentActivity extends BaseActivity {

    private int order;

    private void initData() {
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null && mBundle.getSerializable("order") != null) {
            order = (int)mBundle.getSerializable("order");
            Log.e("VideoAndDocumentActivity","the order is "+ order);
        }else{
            Log.e("VideoAndDocumentActivity","the order is none");
            Toast t = Toast.makeText(this,"参数错误，即将返回", Toast.LENGTH_LONG);
            t.show();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            onBackPressed();
        }
    }

    protected void initDocument(){
        String[] wordUrls = {
                this.getString(R.string.word_url1),this.getString(R.string.word_url2),
                this.getString(R.string.word_url3),this.getString(R.string.word_url4),
                this.getString(R.string.word_url5),this.getString(R.string.word_url6),
                this.getString(R.string.word_url7),this.getString(R.string.word_url8)
        };
        WebView webView =fd(R.id.document_web_view2);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
        });
        // webview必须设置支持Javascript才可打开
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置此属性,可任意比例缩放
        webView.getSettings().setUseWideViewPort(true);
        Log.e("DocumentActivity","ready to load html");
        //通过在线预览office文档的地址加载
        webView.loadUrl("https://view.officeapps.live.com/op/view.aspx?src="+wordUrls[order]);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_document);
        initData();
        String[] arrWays = {
                this.getString(R.string.way1),this.getString(R.string.way2),
                this.getString(R.string.way3),this.getString(R.string.way4),
                this.getString(R.string.way5),this.getString(R.string.way6),
                this.getString(R.string.way7),this.getString(R.string.way8)
        };
        initNavBar(false,arrWays[order]);
        initDocument();
    }
}
